<?php
/* @var $this DeliveryReceiptController */
/* @var $model DeliveryReceipt */

$this->breadcrumbs=array(
	'Delivery Receipts'=>array('index'),
	$model->id,
);

$this->menu=array(
    array('label'=>'Add Delivery Receipt Details', 'url'=>array('/deliveryReceiptDetails/create', 'id'=>$model->id)),
	array('label'=>'Return to Purchase Order', 'url'=>array('/purchaseOrder/view', 'id'=>$model->purchase_order_id)),
	//array('label'=>'List DeliveryReceipt', 'url'=>array('index')),
	//array('label'=>'Create DeliveryReceipt', 'url'=>array('create')),
	array('label'=>'Update DeliveryReceipt', 'url'=>array('update', 'id'=>$model->id)),
	array('label'=>'Delete DeliveryReceipt', 'url'=>'#', 'linkOptions'=>array('submit'=>array('delete','id'=>$model->id),'confirm'=>'Are you sure you want to delete this item?')),
	array('label'=>'Manage DeliveryReceipt', 'url'=>array('admin')),
);


?>

<h1>Delivery Receipt #<?php echo $model->id; ?></h1>

<?php $this->widget('zii.widgets.CDetailView', array(
	'data'=>$model,
	'attributes'=>array(
		'purchase_order_id',
		'delivery_date',
		'site_delivery',
		
	),
)); ?>

<br><br>
<h2>Delivery Receipt Items</h2>
	
	<?php 
$config = array();
$dataProvider = new CArrayDataProvider($rawData=$model->deliveryReceiptDetails, $config);

$this->widget('zii.widgets.grid.CGridView', array('dataProvider'=>$dataProvider, 'columns'=>array(
        array('name'=>'item_code', 'header'=> 'Item Code', 'value'=>'$data->item->item_code'),
		array('name'=>'item_id', 'header'=> 'Item', 'value'=>'$data->item->item_desc'),
		array('name' => 'Quantity', 'value' => '$data->qty' .' ." " .$data->item->unit ' ),
		array('name' => 'Amount', 'value' =>  ' "Php" ." " .$data->amount'),
		array(
            'class'=>'CButtonColumn'
            , 'viewButtonUrl'=>'Yii::app()->createUrl("/DeliveryReceiptDetails/view", array("id"=>$data["id"]))'
            , 'updateButtonUrl'=>'Yii::app()->createUrl("/DeliveryReceiptDetails/update", array("id"=>$data["id"]))'
            , 'deleteButtonUrl'=>'Yii::app()->createUrl("/DeliveryReceiptDetails/delete", array("id"=>$data["id"]))'
            )
    )
));

	if(isset($_GET['deliveryReceiptDetailsid'])){
$deliveryReceiptDetailsid = $_GET['deliveryReceiptDetailsid']; 
$sql = "Select * from delivery_receipt_details where id = '$deliveryReceiptDetailsid'";
$dataReader =  Yii::app()->db->createCommand($sql)->query();
while(($row = $dataReader->read())!==false) {$pull = $row;}
$item_id=$pull['item_id'];

$sql2 = "Select unit_price from item where id = '$item_id'";
$dataReader =  Yii::app()->db->createCommand($sql2)->query();
while(($row = $dataReader->read())!==false) {$pull2 = $row;}

$request_amount = ($pull['qty'] * $pull2['unit_price']);

$sql1 = "update delivery_receipt_details set amount='$request_amount' where id= '$deliveryReceiptDetailsid'";
$dataReader =  Yii::app()->db->createCommand($sql1)->query();
		
 $this->redirect(array('/deliveryReceipt/view','id'=> $model->id));
		
		}else{
    	echo '';
		}


$total = Yii::app()->db->createCommand("SELECT SUM(`amount`) AS `amount` FROM `delivery_receipt_details` where `delivery_receipt_id` = '$model->id'")->queryAll();


if(isset($total[0]['amount'])){
echo '<div id="yw3" class="grid-view">';
 echo '<table class="items">';
      echo '<th>Total Amount</th><td class ="odd">'.' Php ' .($total[0]['amount']).'</td></tr>'; 
	  echo '</table></div>';
		}else{
    	echo '';}

		
		
?>


<?php $en=deliveryReceipt::model()->findAll('id = :a', array(':a'=>$model->id));?>
	<?php if (count($en) !== 0){?>
	<br>
	</center>
	<h2>Purchase Order</h2>
	<?php foreach ($en as $row) { ?>
		<?php echo CHtml::link ( 'Update', array('purchaseOrder/update', 'id'=>$row->purchase_order_id)); ?>
			<?php $this->widget ('zii.widgets.CdetailView', array('data'=>$row,'attributes'=>array(
	        array('label'=>'Supplier Id' , 'value'=>$row->purchaseOrder->supplier->supplier_name),
			array('label'=>'Project Id' , 'value'=>$row->purchaseOrder->project->project_name),
			),));?><br>
	<?php }} ?>
	
			
<?php $en=deliveryReceipt::model()->findAll('id = :a', array(':a'=>$model->id));?>
	<?php if (count($en) !== 0){?>
	<br>
	<h2>Project</h2>
	<?php foreach ($en as $row) { ?>
		<?php echo CHtml::link ( 'Update', array('supplier/update', 'id'=>$row->purchase_order_id)); ?>
			<?php $this->widget ('zii.widgets.CdetailView', array('data'=>$row,'attributes'=>array(
	        array('label'=>'Project Id' , 'value'=>$row->purchaseOrder->project->project_name),
			array('label'=>'Point Person' , 'value'=>$row->purchaseOrder->project->point_person),
			array('label'=>'Start Date' , 'value'=>$row->purchaseOrder->project->start_date),
			array('label'=>'End Date' , 'value' =>$row->purchaseOrder->project->end_date),
			array('label'=>'Total Cost' , 'value' =>'Php ' .$row->purchaseOrder->project->total_cost),
			array('label'=>'Total Expense' , 'value' =>'Php ' .$row->purchaseOrder->project->total_expenses),
			
			),));?><br>
	<?php }} ?>
	
	
<?php $en=deliveryReceipt::model()->findAll('id = :a', array(':a'=>$model->id));?>
	<?php if (count($en) !== 0){?>
	<br>
	<h2>Supplier</h2>
	<?php foreach ($en as $row) { ?>
		<?php echo CHtml::link ( 'Update', array('supplier/update', 'id'=>$row->purchase_order_id)); ?>
			<?php $this->widget ('zii.widgets.CdetailView', array('data'=>$row,'attributes'=>array(
	        array('label'=>'Supplier Id' , 'value'=>$row->purchaseOrder->supplier->supplier_name),
			array('label'=>'Contact Person' , 'value'=>$row->purchaseOrder->supplier->contact_person),
			array('label'=>'Mobile Number' , 'value'=>$row->purchaseOrder->supplier->mobile_no),
			array('label'=>'Fax' , 'value' =>$row->purchaseOrder->supplier->fax),
			array('label'=>'Email' , 'value' =>$row->purchaseOrder->supplier->email),
			),));?><br>
	<?php }} ?>
	
